package model.parser;

import java.awt.Point;

import org.jdom.Element;

public class NativeObjectsPersistor {
	
	//---------NATIVE OBJECTS-----------
	private void persistNumber(Element parent,String elementName,Number value){
		persistString(parent,elementName,String.valueOf(value));
	}
	
	public void persistInteger(Element parent,String elementName,int value){
		persistNumber(parent,elementName,value);
	}
	
	public int loadInteger(Element intElement){
		return Integer.parseInt(intElement.getValue());
	}
	
	public void persistLong(Element parent,String elementName,long value){
		persistNumber(parent,elementName,value);
	}
	
	public long loadLong(Element longElement){
		return Long.parseLong(longElement.getValue());
	}
	
	public void persistFloat(Element parent,String elementName,float value){
		persistNumber(parent,elementName,value);
	}
	
	public float loadFloat(Element floatElement){
		return Float.parseFloat(floatElement.getValue());
	}
	
	public void persistChar(Element parent,String elementName,char value){
		Element charElement = new Element(elementName);
		charElement.addContent(String.valueOf(value));
		parent.addContent(charElement);
	}
	
	public void persistString(Element parent,String elementName,String value){
		Element strElement = new Element(elementName);
		strElement.addContent(value);
		parent.addContent(strElement);
	}
	
	public String loadString(Element strElement){
		return strElement.getValue();
	}
	
	public char loadChar(Element strElement){
		String value = (String)strElement.getValue();
		if(value != null && value.length()>0){
			return value.charAt(0);
		}else{
			return ' ';
		}
	}
	
	public void persistBoolean(Element parent,String elementName,Boolean value){
		persistString(parent,elementName,String.valueOf(value));
	}
	
	public Boolean loadBoolean(Element booleanElement){
		return Boolean.parseBoolean(booleanElement.getValue());
	}
	
	public void persistPoint(Element parent,String elementName,Point point){
		Element pointElement = new Element(elementName);
		persistInteger(pointElement,"X",point.x);
		persistInteger(pointElement,"Y",point.y);
		parent.addContent(pointElement);
	}
	
	public Point loadPoint(Element pointElement){
		Point resultPoint = new Point();
		resultPoint.x = loadInteger(pointElement.getChild("X"));
		resultPoint.y = loadInteger(pointElement.getChild("Y"));
		return resultPoint;
	}
	//------ END OF NATIVE OBJECTS ----
	
}
